import 'dart:convert';

import 'package:bot_toast/bot_toast.dart';
import 'package:exams_app/gen_a/A.dart';
import 'package:exams_app/net/Address.dart';
import 'package:exams_app/net/DioManager.dart';
import 'package:exams_app/net/persisten_storage.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';

class HelpLogic extends GetxController{

  int page = 0;

  Rx<HelpModel> model;

  EasyRefreshController esController = EasyRefreshController();

  var dataList = List();


  /// 空数据

  emptyData(){
    if(dataList.length == 0){
      return Container(
        // width: double.infinity,
        // height: double.infinity,
        child: Center(
          child: SizedBox(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Container(

                    child: Column(
                      children: [
                        Image.asset(A.assets_nodata_icon,width: 100,height: 100,),
                        Text('暂无数据',style: TextStyle(fontSize: 13,fontWeight: FontWeight.w600),),
                      ],
                    )
                ),
              ],
            ),
          ),
        ),
      );
    }else{
      return null;
    }
  }

  /// 下拉刷新

  Future<void> onRefresh()async{
    page = 0;
    requestData(true,page: page);
  }
  ///  上拉加载更多
  Future<void> onLoad()async{
    page++;
    requestData(false,page: page);
    await Future.delayed(Duration(milliseconds: 250), () {

    });
  }

  /// 列表
  void requestData(bool isHeader,{int page = 0})async{

    var params = {
      'limit': 10,                       //   #每页显示数量
      'order': 'id desc',               //    #排序
      'page': page,
    };
    String result = await DioUtils(url: await PersistentStorage().getStorage('urlStr')).postModel(Address.getattt,model: 'res.help',function_name: '_api_page',params:params );

    Map json = jsonDecode(result);

    model = HelpModel.fromJson(json).obs;


    if(page == 0){

      esController.resetLoadState();
      dataList.clear();
      dataList.addAll(model.value.data);
    }else{

      if(json['data'].length>0){
        // for(int i = 0;i<model.data.length;i++){
        //   model.data[i].isSelect = false;
        // }

        dataList.addAll(model.value.data);
      }else if(json['data'].length <= 0||json['data']==null){
        // _esController.finishLoad(success: true,noMore: true);
        esController.finishLoad(success: true, noMore: true);
        BotToast.showText(text: '暂无更多');
      }
    }

    update();
  }

  @override
  void onInit() {
    // TODO: implement onInit
    requestData(true,page: 0);
    super.onInit();


  }
}

class HelpModel {
  String search;
  List<Data> data;
  int searchCount;
  int errcode;
  String errmsg;
  String message;

  HelpModel(
      {this.search,
        this.data,
        this.searchCount,
        this.errcode,
        this.errmsg,
        this.message});

  HelpModel.fromJson(Map<String, dynamic> json) {
    search = json['search'];
    if (json['data'] != null) {
      data = new List<Data>();
      json['data'].forEach((v) {
        data.add(new Data.fromJson(v));
      });
    }
    searchCount = json['search_count'];
    errcode = json['errcode'];
    errmsg = json['errmsg'];
    message = json['message'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['search'] = this.search;
    if (this.data != null) {
      data['data'] = this.data.map((v) => v.toJson()).toList();
    }
    data['search_count'] = this.searchCount;
    data['errcode'] = this.errcode;
    data['errmsg'] = this.errmsg;
    data['message'] = this.message;
    return data;
  }
}

class Data {
  String createDate;
  int id;
  String name;
  String describe;

  Data({this.createDate, this.id, this.name, this.describe});

  Data.fromJson(Map<String, dynamic> json) {
    createDate = json['create_date'];
    id = json['id'];
    name = json['name'];
    describe = json['describe'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['create_date'] = this.createDate;
    data['id'] = this.id;
    data['name'] = this.name;
    data['describe'] = this.describe;
    return data;
  }
}